package cn.ghx.xboot.front;

import cn.ghx.xboot.brand.BrandService;
import cn.ghx.xboot.brand.dto.BrandCategoryDto;
import cn.ghx.xboot.client.Client;
import cn.ghx.xboot.client.ClientService;
import cn.ghx.xboot.common.BaseContext;
import cn.ghx.xboot.common.Result;
import cn.ghx.xboot.front.vo.OrderDemandVo;
import cn.ghx.xboot.item.Item;
import cn.ghx.xboot.item.ItemService;
import cn.ghx.xboot.item.fav.ItemFavUserService;
import cn.ghx.xboot.order.appointment.OrderAppointment;
import cn.ghx.xboot.order.appointment.OrderAppointmentService;
import cn.ghx.xboot.order.demand.OrderDemand;
import cn.ghx.xboot.order.demand.OrderDemandService;
import cn.ghx.xboot.order.price.OrderPrice;
import cn.ghx.xboot.order.price.OrderPriceService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 针对小程序端的接口
 */
@RestController
@RequestMapping("/front")
public class FrontController {
    public static final String GET_BRAND = "/brand";
    public static final String POST_USERINFO = "/userinfo";

    public static final String GET_ITEM_LIST = "/item/list";
    public static final String GET_ITEM_BY_ID = "/item/{id}";
    public static final String GET_ITEM_RECOMMEND = "/item/recommend";
    public static final String PUT_ITEM_FAV = "/item/fav";
    public static final String POST_ORDER_DEMAND = "/order/demand";
    public static final String POST_ORDER_PRICE = "/order/price";
    public static final String POST_ORDER_APPOINTMENT = "/order/appointment";
    public static final String GET_MINE_ORDER_DEMAND = "/mine/order/demand";
    public static final String GET_MINE_ORDER_PRICE = "/mine/order/price";
    public static final String GET_MINE_ORDER_APPOINTMENT = "/mine/order/appointment";
    public static final String GET_MINE_FAV = "/mine/fav";

    private final BrandService brandService;
    private final ClientService clientService;
    private final ItemService itemService;
    private final OrderDemandService orderDemandService;
    private final OrderPriceService orderPriceService;
    private final OrderAppointmentService orderAppointmentService;
    private final ItemFavUserService itemFavUserService;

    @Lazy
    public FrontController(BrandService brandService,
                           ItemService itemService,
                           OrderDemandService orderDemandService,
                           ClientService clientService,
                           OrderPriceService orderPriceService,
                           OrderAppointmentService orderAppointmentService,
                           ItemFavUserService itemFavUserService) {
        this.brandService = brandService;
        this.clientService = clientService;
        this.itemService = itemService;
        this.orderDemandService = orderDemandService;
        this.orderPriceService = orderPriceService;
        this.orderAppointmentService = orderAppointmentService;
        this.itemFavUserService = itemFavUserService;
    }


    @PutMapping(PUT_ITEM_FAV)
    public Result<Boolean> toggleItemFav(@RequestParam String id, @RequestParam Boolean fav) {
        return Result.success(itemFavUserService.toggleFav(id, fav));
    }

    @GetMapping(GET_ITEM_RECOMMEND)
    public Result<List<Item>> getRecommend(
            @RequestParam String originId,
            @RequestParam(required = false) Integer size
    ) {
        return Result.success(itemService.getRecommend(originId, size));
    }

    @GetMapping(GET_MINE_ORDER_DEMAND)
    public Result<Page<OrderDemand>> getMineOrderDemand(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size
    ) {
        String openid = BaseContext.getOpenid();
        Assert.isTrue(StringUtils.hasText(openid), "请先登录");
        return Result.success(orderDemandService.query(keyword, null, null, null, openid, null, page, size));
    }

    @GetMapping(GET_MINE_ORDER_PRICE)
    public Result<Page<OrderPrice>> getMineOrderPrice(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size
    ) {
        String openid = BaseContext.getOpenid();
        Assert.isTrue(StringUtils.hasText(openid), "请先登录");
        return Result.success(orderPriceService.query(keyword, openid, null, null,page, size));
    }

    @GetMapping(GET_MINE_ORDER_APPOINTMENT)
    public Result<Page<OrderAppointment>> getMineOrderAppointment(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size
    ) {
        String openid = BaseContext.getOpenid();
        Assert.isTrue(StringUtils.hasText(openid), "请先登录");
        return Result.success(orderAppointmentService.query(keyword, openid, null,null, page, size));
    }

    @GetMapping(GET_MINE_FAV)
    public Result<Page<Item>> getMineFav(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size
    ) {
        String openid = BaseContext.getOpenid();
        Assert.isTrue(StringUtils.hasText(openid), "请先登录");
        return Result.success(itemFavUserService.query(keyword, openid, page, size));
    }

    @PostMapping(POST_ORDER_DEMAND)
    public Result<Boolean> saveOrderDemand(@RequestBody OrderDemandVo item) {
        return Result.success(orderDemandService.saveVo(item));
    }
    @PostMapping(POST_ORDER_PRICE)
    public Result<Boolean> saveOrderPrice(@RequestBody OrderPrice item) {
        String openid = BaseContext.getOpenid();
        Assert.isTrue(StringUtils.hasText(openid),"请先登录");
        item.setOpenid(openid);
        return Result.success(orderPriceService.saveItem(item));
    }
    @PostMapping(POST_ORDER_APPOINTMENT)
    public Result<Boolean> saveOrderAppointment(@RequestBody OrderAppointment item) {
        String openid = BaseContext.getOpenid();
        Assert.isTrue(StringUtils.hasText(openid),"请先登录");
        item.setOpenid(openid);
        return Result.success(orderAppointmentService.saveItem(item));
    }

    @GetMapping(GET_ITEM_BY_ID)
    public Result<Item> getItemById(@PathVariable String id) {
        String openid = BaseContext.getOpenid();
        return Result.success(itemService.detail(id, openid));
    }

    @GetMapping(GET_ITEM_LIST)
    public Result<Page<Item>> getItemList(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String brandId,
            @RequestParam(required = false) String storeId,
            @RequestParam(required = false) Float tonnageStart,
            @RequestParam(required = false) Float tonnageEnd,
            @RequestParam(required = false) Float priceStart,
            @RequestParam(required = false) Float priceEnd,
            @RequestParam(required = false) Integer hourStart,
            @RequestParam(required = false) Integer hourEnd,
            @RequestParam(required = false) Boolean rentable,
            @RequestParam(required = false) String province,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size,
            @RequestParam(required = false, defaultValue = "sort") String sort
    ) {
        return Result.success(itemService.query(
                keyword,
                brandId, storeId, tonnageStart, tonnageEnd, priceStart, priceEnd, hourStart, hourEnd, rentable,
                province,
                page, size, sort));
    }

    @PostMapping(POST_USERINFO)
    public Result<Boolean> saveUserInfo(@RequestBody Client item) throws Exception {
        String openid = item.getOpenid();
        Assert.isTrue(Objects.equals(openid, BaseContext.getOpenid()), "非法修改他人信息");
        return Result.success(clientService.saveOrUpdate(item));
    }

    @GetMapping(GET_BRAND)
    public Result<List<BrandCategoryDto>> getAll() {
        return Result.success(brandService.getAll());
    }
}
